package com.eleadmin.store.service.impl;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
//import com.eleadmin.store.mapper.SellMapper;
import com.eleadmin.damon.utils.utils.ArithUtil;
import com.eleadmin.store.biz.ProductStorageBiz;
import com.eleadmin.store.biz.SellSonBiz;
import com.eleadmin.store.entity.*;
import com.eleadmin.store.service.SellService;
import org.springframework.stereotype.Service;
import com.eleadmin.store.biz.SellBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售单服务实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-03-07 14:12:18
 */
@Service
public class SellServiceImpl /*extends ServiceImpl<SellMapper, Sell> */ implements SellService {

    @Autowired
    private SellBiz sellBiz;
    @Autowired
    private SellSonBiz sellSonBiz;
    @Autowired
    private ProductStorageBiz productStorageBiz;

    @Override
    public JsonResult list(PageResult2<T> page, Sell sell, User loginUser) throws Exception {
        JsonResult jr = sellBiz.list(page, sell, loginUser);
        jr.put("priceResult", sellBiz.sellNums(sell, loginUser));
        return jr;
    }

    @Override
    public JsonResult sellNums(Sell sell, User loginUser) {
        return JsonResult.ok().setData(sellBiz.sellNums(sell, loginUser));
    }

    @Override
    public JsonResult list(Sell sell, User loginUser) {
        return JsonResult.ok().setData(sellBiz.list(sell, loginUser));
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(sellBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(Sell sell, User loginUser) {
        return JsonResult.ok("添加成功").setData(sellBiz.save(sell, loginUser));
    }

    @Transactional
    @Override
    public JsonResult update(Sell sell, User loginUser) {
        if (sellBiz.update(sell, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updateTableState(Sell sell, User loginUser) {
        if (sellBiz.updateTableState(sell, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(String id, User loginUser) {
        if (sellBiz.remove(id, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult removeSellAndSon(String id, User loginUser) throws Exception {
        String[] ids = id.split(", ");
        //多个入库主表id
        if (ids != null && ids.length > 0) {
            for (int j = 0; j < ids.length; j++) {
                SellSon sellSon = new SellSon();
                sellSon.setSellid(Integer.valueOf(ids[j]));
                //根据主表id获取子表所有记录
                List<SellSon> list = sellSonBiz.list(sellSon, loginUser);
                SellSon sellSon2 = null;
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        sellSon2 = list.get(i);

                        //修改库存
                        ProductStorage ps = new ProductStorage();
                        ps.setWarehousename(sellSon2.getWarehousename());
                        ps.setProductname(sellSon2.getProductname());
                        ps.setSpecification(sellSon2.getSpecification());
                        ps.setLengths(sellSon2.getLengths() + "");
                        ps.setNegative(sellSon2.getNegative() + "");
                        ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
                        try {
                            byName.setNum(Double.valueOf(sellSon2.getNum()));
                            byName.setLitotalweight(Double.valueOf(sellSon2.getLitotalweight()));
                            productStorageBiz.updateNum(byName, loginUser);
                        } catch (Exception e) {
                            throw new Exception("没有对应的库存数据！");
                        }
                        //删除子表
                        sellSonBiz.remove(sellSon2.getId(), loginUser);
                    }
                }
                //主表 总金额更新
                Double total = sellSonBiz.calcTotalPrice(sellSon, loginUser);
                Sell sell = new Sell(sellSon.getSellid() + "", total + "");
                sellBiz.updateTotal(sell, loginUser);
                //删除主表
                sellBiz.remove(id, loginUser);
            }
        }

        return JsonResult.ok("删除成功");
    }

}
